// 用户数据管理
const UserManager = {
    // 获取当前登录用户
    getCurrentUser() {
        return JSON.parse(localStorage.getItem('currentUser') || '{}');
    },
    
    // 检查是否已登录
    isLoggedIn() {
        const user = this.getCurrentUser();
        return !!user.username;
    },
    
    // 获取用户详细信息
    getUserProfile(username) {
        const registeredUsers = JSON.parse(localStorage.getItem('registeredUsers') || '[]');
        return registeredUsers.find(user => user.username === username) || {};
    },
    
    // 获取用户统计数据
    getUserStats(username) {
        const userStats = JSON.parse(localStorage.getItem('userStats') || '{}');
        return userStats[username] || {
            posts: 0,
            comments: 0,
            follows: 0,
            points: 100
        };
    },
    
    // 初始化用户统计数据
    initUserStats() {
        if (!localStorage.getItem('userStats')) {
            const registeredUsers = JSON.parse(localStorage.getItem('registeredUsers') || '[]');
            const userStats = {};
            
            registeredUsers.forEach(user => {
                userStats[user.username] = {
                    posts: Math.floor(Math.random() * 20),
                    comments: Math.floor(Math.random() * 50),
                    follows: Math.floor(Math.random() * 30),
                    points: 100 + Math.floor(Math.random() * 200)
                };
            });
            
            localStorage.setItem('userStats', JSON.stringify(userStats));
        }
    },
    
    // 退出登录
    logout() {
        localStorage.removeItem('currentUser');
    }
};

// 初始化用户统计数据
document.addEventListener('DOMContentLoaded', function() {
    UserManager.initUserStats();
});